package com.jereh.cloudmusic.actvity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.widget.RemoteViews;

import com.jereh.cloudmusic.PlayApplication;
import com.jereh.cloudmusic.R;
import com.jereh.cloudmusic.constants.Constants;
import com.jereh.cloudmusic.entity.Bitrate;
import com.jereh.cloudmusic.entity.Song;
import com.jereh.cloudmusic.entity.SongInfo;
import com.jereh.cloudmusic.ible.IMusic;
import com.jereh.cloudmusic.service.PlayerService;

import java.util.ArrayList;
import java.util.List;

public class SongDetailActivity extends AppCompatActivity {

    private RemoteViews contentView;

    private Notification notification;
    private PlayApplication application;
    private List<Song> list=new ArrayList<>();
    private Intent intent;
    private IMusic music;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_detail);
        Song song=new Song();
        Bitrate bitrate=new Bitrate();
        bitrate.setFile_link("http://yinyueshiting.baidu.com/data2/music/42783748/42783748.mp3?xcode=f8892c3c8e1752c0c9bf3d409e407d1c");
        SongInfo info=new SongInfo();
        info.setTitle("海阔天空");
        song.setBitrate(bitrate);
        song.setSonginfo(info);
        Song song2=new Song();
        Bitrate bitrate2=new Bitrate();
        bitrate2.setFile_link("http://yinyueshiting.baidu.com/data2/music/f82dd0820651c18860dced6d44d98681/277581711/277581711.mp3?xcode=23a4d936fc1a6b4c0d9b8f0c677c76d2");
        SongInfo info2=new SongInfo();
        info2.setTitle("你在就好");
        song2.setBitrate(bitrate2);
        song2.setSonginfo(info2);
        Song song3=new Song();
        Bitrate bitrate3=new Bitrate();
        bitrate3.setFile_link("http://yinyueshiting.baidu.com/data2/music/6d0f1b7d5070e3e76c60ed4bfae13edc/278862684/278862684.mp3?xcode=e96de29b7a2d14bb9e9cdd6067ee2141");
        SongInfo info3=new SongInfo();
        info3.setTitle("长大的童话");
        song3.setBitrate(bitrate3);
        song3.setSonginfo(info3);
        list.add(song);
        list.add(song2);
        list.add(song3);
        application = (PlayApplication) getApplication();
        application.songsList.addAll(list);
        application.notiManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        initService();
        // 初始化通知栏播放控件
        initNotificationBar();

    }

    private void initService(){
        intent=new Intent(this, PlayerService.class);
        startService(intent);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            PlayerService.PlayerBinder binder=(PlayerService.PlayerBinder)service;
            music=binder;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onDestroy() {
        unbindService(connection);
        super.onDestroy();
    }

    private void initNotificationBar() {
        notification = new Notification();
        //初始化通知
        notification.icon = R.mipmap.ic_launcher;
        contentView = new RemoteViews(getPackageName(),
                R.layout.notification_control);
        notification.contentView = contentView;
        //新建意图，并设置action标记为"play"，用于接收广播时过滤意图信息
        Intent intentPlay = new Intent("play");
        PendingIntent pIntentPlay = PendingIntent.getBroadcast(this, 0,
                intentPlay, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_play, pIntentPlay);//为play控件注册事件

        Intent intentPause = new Intent("pause");
        PendingIntent pIntentPause = PendingIntent.getBroadcast(this, 0,
                intentPause, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_pause, pIntentPause);

        Intent intentNext = new Intent("next");
        PendingIntent pIntentNext = PendingIntent.getBroadcast(this, 0,
                intentNext, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_next, pIntentNext);

        Intent intentLast = new Intent("last");
        PendingIntent pIntentLast = PendingIntent.getBroadcast(this, 0,
                intentLast, 0);
        contentView.setOnClickPendingIntent(R.id.bt_notic_last, pIntentLast);

        Intent intentCancel = new Intent("cancel");
        PendingIntent pIntentCancel = PendingIntent.getBroadcast(this, 0,
                intentCancel, 0);
        contentView
                .setOnClickPendingIntent(R.id.bt_notic_cancel, pIntentCancel);
        notification.flags = notification.FLAG_NO_CLEAR;//设置通知点击或滑动时不被清除
        application.notiManager.notify(Constants.PLAY_NOTI_ID, notification);//开启通知
    }
}
